package shark.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shark.user.model.SysDepartTreeModel;
import shark.user.service.ISysDepartService;
import shark.user.spi.entity.SysDepart;
import shark.user.spi.mapper.SysDepartMapper;

import java.util.*;

@Service
public class ISysDepartServiceImpl extends ServiceImpl<SysDepartMapper, SysDepart> implements ISysDepartService {

    @Autowired
    private SysDepartMapper sysDepartMapper;

    @Override
    public List<SysDepartTreeModel> queryMyDeptTreeList(String departIds) {
        LambdaQueryWrapper<SysDepart> query = new LambdaQueryWrapper<SysDepart>();
        query.eq(SysDepart::getDelFlag, String.valueOf(0));
        query.in(SysDepart::getId, Arrays.asList(departIds.split(",")));
        query.orderByAsc(SysDepart::getOrgCode);
        List<SysDepart> list = this.list(query);
        List<SysDepart> all = this.list(new LambdaQueryWrapper<SysDepart>().eq(SysDepart::getDelFlag, String.valueOf(0)));

        List<SysDepart> result = new ArrayList<>();
        for (SysDepart sysDepart:list){
            this.getChild(all, sysDepart.getId(), result);
        }
        result.addAll(list);

        return getTree(result);
    }

    @Override
    public List<SysDepartTreeModel> queryTreeList() {
        List<SysDepart> metaList =
                sysDepartMapper.selectList(new LambdaQueryWrapper<SysDepart>()
                        .eq(SysDepart::getDelFlag, String.valueOf(0))
                        .orderByAsc(SysDepart::getDepartOrder));
        return getTree(metaList);
    }

    private List<SysDepartTreeModel> getTree(List<SysDepart> departs) {
        List<SysDepartTreeModel> root = new ArrayList<>();
        List<SysDepartTreeModel> tree = new ArrayList<>();

        for (SysDepart sysDepart : departs) {
            if (sysDepart.getParentId() == null || "".equals(sysDepart.getParentId())) {
                root.add(new SysDepartTreeModel(sysDepart));
            } else {
                tree.add(new SysDepartTreeModel(sysDepart));
            }
        }

        getChildDepart(root, tree);
        setEmptyChildrenAsNull(root);
        return root;
    }

    private void getChildDepart (List<SysDepartTreeModel> root, List<SysDepartTreeModel> tree) {
        for (int i = 0; i<root.size(); i++) {
            SysDepartTreeModel rootBranch =root.get(i);
            for (int j = 0; j<tree.size(); j++) {
                SysDepartTreeModel node = tree.get(j);
                if (node.getParentId() != null && node.getParentId().equals(rootBranch.getId())) {
                    rootBranch.getChildren().add(node);
                }
            }
            if (rootBranch.getChildren() != null) {
                getChildDepart(rootBranch.getChildren(),tree);
            }
        }
    }

    private void setEmptyChildrenAsNull(List<SysDepartTreeModel> tree){
        for (int i = 0; i < tree.size(); i++) {
            SysDepartTreeModel model = tree.get(i);
            if (model.getChildren().size() == 0) {
                model.setChildren(null);
                model.setIsLeaf(true);
            }else{
                setEmptyChildrenAsNull(model.getChildren());
                model.setIsLeaf(false);
            }
        }
    }

    private void getChild(List<SysDepart> all, String pid, List<SysDepart> result) {
        for (SysDepart depart : all) {
            if (pid.equals(depart.getParentId())) {
                getChild(all, depart.getId(), result);
                result.add(depart);
            }
        }
    }
}
